//
//  LoginViewController.m
//  WuJieLive
//
//  Created by iMac on 2017/10/18.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "LoginViewController.h"
#import "ForgetPasswordViewController.h"
#import "RegisterViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UIButton *safeBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)setNavi
{
    self.edgesForExtendedLayout = UIRectEdgeNone;//  是表视图从导航条下开始布局显示
    self.title = @"登录";
    [self.leftBtn setImage:Image(@"icon_nav_关闭") forState:(UIControlStateNormal)];
}


- (void)leftBtnAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)setUI
{
    [_passwordText setValue:TextInputColor forKeyPath:@"_placeholderLabel.textColor"];
    [_passwordText setValue:FontSize(15) forKeyPath:@"_placeholderLabel.font"];
  
    [_phoneText setValue:TextInputColor forKeyPath:@"_placeholderLabel.textColor"];
    [_phoneText setValue:FontSize(15) forKeyPath:@"_placeholderLabel.font"];
    
    [_passwordText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    [_phoneText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    
}

- (void)textFieldDidChange:(UITextField *)textField
{
    if (_passwordText.text.length >= 6 && _phoneText.text.length == 11) {
        self.loginBtn.userInteractionEnabled = YES;
        [self.loginBtn setBackgroundColor:MainColor];
    }else
    {
        [self.loginBtn setBackgroundColor:MainBtnColor];
        self.loginBtn.userInteractionEnabled = NO;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

#pragma mark 用户注册操作
- (IBAction)userRegister:(id)sender {
    
    RegisterViewController * registerVC = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

#pragma mark 登录
- (IBAction)loginAction:(id)sender {
   [MBProgressHUD showHUDAddedTo:self.view animated:YES];
   NSMutableDictionary * dict = [NSMutableDictionary dictionary];
   [dict setObject:self.phoneText.text forKey:@"phone_mob"];
   [dict setObject:self.passwordText.text forKey:@"password"];
   [HTTPTool POSTForm:[NSString stringWithFormat:@"%@member/login",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
      [MBProgressHUD hideHUDForView:self.view animated:YES];
      if (isSuccess) {
         self.isLogin = YES;
         self.userToken = responseObject [@"data"][@"token"];
         [self dismissViewControllerAnimated:YES completion:nil];
      }
   } failure:^(NSError *error) {
      [MBProgressHUD hideHUDForView:self.view animated:YES];
   }];
   
}

- (IBAction)isSafePassword:(UIButton *)sender {
    
    sender.selected = !sender.isSelected;
    _passwordText.secureTextEntry = !sender.isSelected;
    
}

#pragma mark 忘记密码操作
- (IBAction)forgetPassword:(id)sender {
    ForgetPasswordViewController * vc = [[ForgetPasswordViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 微信登录
- (IBAction)weixinLogin:(id)sender {
}
#pragma mark QQ登录
- (IBAction)QQLogin:(id)sender {
}
#pragma mark 新浪微博登录
- (IBAction)sinaLogin:(id)sender {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
